package com.buildings.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.buildings.BuildConfig;
import com.buildings.R;
import com.buildings.adapter.ProjectListAdapter;
import com.buildings.adapter.TaskListAdapter;
import com.buildings.api.Api;
import com.buildings.base.BaseActivity;
import com.buildings.bean.ProjectBean;
import com.buildings.bean.ProjectListBean;
import com.buildings.bean.TaskBean;
import com.buildings.bean.TaskListBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.SPUtil;
import com.buildings.view.SelectTypeFragmentDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ProjectListActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    private TextView tvTitle;
    private int pageNum = 1;
    private int total = 10;
    private ProjectListAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_list);
        ButterKnife.bind(this);
        initView();
        initAdapter();
        http();
    }

    @OnClick({R.id.iv_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mAdapter = new ProjectListAdapter(this, datas);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new ProjectListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(activity, TaskListActivity.class);
                intent.putExtra("projectId", datas.get(position).getProjectId());
                intent.putExtra("projectName", datas.get(position).getProjectName());
                startActivity(intent);
            }
        });
    }

    private void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("项目列表");
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (datas.size() >= total) {
                    CustomToast.showToast(ProjectListActivity.this, "没有更多的任务");
                    refreshlayout.finishLoadmore();
                } else {
                    //todo 网络请求
                    pageNum++;
                    http();
                }
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                //todo 网络请求
                http();
            }
        });
    }

    private List<ProjectBean> datas = new ArrayList<>();

    private void http() {
        String url = Api.QUERY_HDD_PROJECT + "?curPage=" + pageNum + "&personId=" + SPUtil.get("userId", "") + "&pageSize=10";
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
//                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<ProjectListBean>() {
                    @Override
                    public void onSuccess(ProjectListBean bean) {
                        super.onSuccess(bean);
                        refreshLayout.finishLoadmore();
                        refreshLayout.finishRefresh();
                        if (bean != null && bean.getCode() == 200) {
                            if (pageNum == 1) {
                                datas.clear();
                            }
                            total = bean.getData().getTotal();
                            List<ProjectBean> result = bean.getData().getRows();
                            datas.addAll(result);
                            mAdapter.notifyDataSetChanged();
                        } else {
                            CustomToast.showToast(ProjectListActivity.this, "获取失败");
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        ProjectListActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                CustomToast.showToast(ProjectListActivity.this, "网络异常");
                            }
                        });
                        refreshLayout.finishLoadmore();
                        refreshLayout.finishRefresh();
                    }
                });
    }
}
